<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <el-input placeholder="请输入硬件名称" clearable prefix-icon="el-icon-search" v-model="modelName">
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      <el-button type="success" icon="el-icon-plus" @click="DialogVisible = true">添加</el-button>
    </div>
    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column align="center" prop="hardwareId" label="硬件ID">
        </el-table-column>
        <el-table-column align="center" prop="hardwareName" label="硬件名称">
        </el-table-column>
        <el-table-column align="center" prop="hardwareKey" label="硬件标识">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加 -->
    <div class="add">
      <el-dialog title="新增" :visible.sync="DialogVisible" width="600px" @close="close">
        <el-form :model="addForm" ref="addForm" label-width="100px" class="demo-ruleForm" :rules="rules">
          <el-form-item label="硬件名称" prop="hardwareName">
            <el-input v-model="addForm.hardwareName" clearable placeholder="请输入硬件名称"></el-input>
          </el-form-item>

          <el-form-item label="硬件标识" prop="hardwareKey">
            <el-input v-model="addForm.hardwareKey" clearable placeholder="请输入硬件标识"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>

    <!-- 编辑 -->
    <div class="edit">
      <el-dialog title="编辑" :visible.sync="editDialogVisible" width="600px">
        <el-form :model="editForm" ref="editForm" label-width="100px" class="demo-ruleForm" :rules="rules">
          <el-form-item label="硬件名称" prop="hardwareName">
            <el-input v-model="editForm.hardwareName" clearable placeholder="请输入硬件名称"></el-input>
          </el-form-item>
          <el-form-item label="硬件标识" prop="hardwareKey">
            <el-input v-model="editForm.hardwareKey" clearable placeholder="请输入硬件标识"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
    
<script>
import {
  getModelApi,
  addModelApi,
  delModelApi,
  editModelApi,
} from "../../../../api/system/hardWare";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: null,
      DialogVisible: false,
      editDialogVisible: false,
      modelName: "",
      addForm: {
        hardwareKey: "",
        hardwareName: "",
      },
      editId: null,
      editForm: {
        hardwareKey: "",
        hardwareName: "",
      },
      rules: {
        hardwareName: [
          { required: true, message: "硬件名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.getModel(this.pageNum, this.pageSize);
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
  },
  methods: {
    // 查询
    search() {
      this.pageNum = 1;
      this.getModel(this.pageNum, this.pageSize, this.modelName);
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 获取模组型号列表
    getModel(pageNum, pageSize, modelName) {
      this.loading = true;
      getModelApi(pageNum, pageSize, modelName).then((res) => {
        console.log(res);
        this.total = res.data.total;
        if (res.data.rows.length == 0) {
          this.pageNum = this.pageNum - 1;
          if (this.pageNum > 0) {
            this.search();
          } else {
            this.tableData = res.data.rows;
            this.loading = false;
          }
        } else {
          this.loading = false;
          this.tableData = res.data.rows;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.pageNum = 1;
      this.getModel(this.pageNum, val);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    handleCurrentChange(val) {
      this.getModel(val, this.pageSize);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 打开修改框
    edit(e) {
      this.editDialogVisible = true;
      this.editForm = {
        hardwareKey: e.hardwareKey,
        hardwareName: e.hardwareName,
      };
      this.editId = e.hardwareId;
    },
    // 修改
    editFn() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          editModelApi(this.editId, this.editForm)
            .then((res) => {
              this.$message.success("修改成功");
              this.search();
              this.editDialogVisible = false;
            })
            .catch((err) => {
              this.$message.error("修改失败");
            });
        } else {
          this.$message.error("模组名称不能为空！");
        }
      });
    },
    // 删除
    delFn(e) {
      this.$confirm("确认删除此硬件？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delModelApi(e.hardwareId)
            .then((res) => {
              this.$message.success("删除成功");
              this.search();
            })
            .catch((err) => {
              this.$message.error("删除失败！");
            });
        })
        .catch((err) => {
          this.$message.success("已取消删除");
        });
    },
    // 添加硬件
    addFn() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          addModelApi(this.addForm)
            .then((res) => {
              this.$message.success("添加成功");
              this.search();
              this.DialogVisible = false;
            })
            .catch((err) => {
              this.$message.error("添加失败");
            });
        } else {
          this.$message.error("硬件名称不能为空！");
        }
      });
    },
    // 关闭添加框
    close() {
      this.addForm = {
        hardwareKey: "",
        hardwareName: "",
      };
    },
  },
};
</script>
    
<style lang="scss" scoped>
.dev {
  padding-top: 15px;
  padding: 0 15px;
  height: calc(100vh - 100px);

  .search {
    margin-bottom: 15px;

    .el-input {
      width: 200px;
    }

    .el-button {
      margin-left: 15px;
    }
  }

  .table {
    height: 100%;

    .el-table {
      height: 70%;
    }

    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>